package com.beep.erp.model.stockdetail;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beep.erp.R;
import com.beep.erp.common.bean.StockListBean;
import com.beep.erp.common.utils.TimeUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jolly on 2018/9/1.
 */

public class InStockListAdapter extends RecyclerView.Adapter<InStockListAdapter.StockDetailViewHolder> {

    //当前上下文对象 需要，，必要参数
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private ArrayList<StockListBean.BizStockInVoListBean> mListBeen;
    private onInStockClickLitener monInStockClickLitener;

    //当前上下文对象mContext必要参数  构造传值
    public InStockListAdapter(Context mContext, ArrayList<StockListBean.BizStockInVoListBean> listBeen) {
        this.mContext = mContext;
        this.mListBeen = listBeen;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setonInStockClickLitener(onInStockClickLitener onItemClickLitener) {
        monInStockClickLitener = onItemClickLitener;
    }

    //初始化布局的地方 new InviteViewHolder 这个是写在下方的 内部类
    @Override
    public StockDetailViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new StockDetailViewHolder(mLayoutInflater.inflate(R.layout.instock_detail_item, parent, false));
    }

    //绑定数据的地方
    @Override
    public void onBindViewHolder(StockDetailViewHolder holder, int position) {
        holder.tvOrderNum.setText(mListBeen.get(position).getStockInCode());
        holder.tvDate.setText(TimeUtils.milliseconds2String(mListBeen.get(position).getStockInDate(), new SimpleDateFormat("yyyy-MM-dd")));
        if (monInStockClickLitener != null) {
            holder.itemView.setOnClickListener(v -> {
                monInStockClickLitener.onInStockClick(v, position);
            });
        }
    }

    //多少条数据
    @Override
    public int getItemCount() {
        return mListBeen.size();
    }

    //上面提到的内部类 如果要使用@BindView ButterKnife.bind(this, itemView);必须要写
    public class StockDetailViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_order_num)
        TextView tvOrderNum;
        @BindView(R.id.tv_date)
        TextView tvDate;

        public StockDetailViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface onInStockClickLitener {
        void onInStockClick(View view, int position);
    }
}
